package com.net.xpay.core.validator;

import com.google.common.base.Strings;
import com.net.common.exception.BusinessException;
import com.net.common.util.SerializationUtil;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.model.CollectionSafePayOrderModel;
import com.net.xpay.core.utils.BankUtil;
import com.net.xpay.core.utils.MoneyCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Slf4j
@Component
public class CollectionSafePayOrderValidator {

    public void validateAddCollectionSafePayOrder(CollectionSafePayOrderModel model) {
        log.info("validateAddCollectionSafePayOrder|model={}", SerializationUtil.obj2String(model));
        if (Strings.isNullOrEmpty(model.getUuid())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "非法提交");
        }
        if (model.getName() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "姓名不能为空");
        }
        model.setName(model.getName().trim());
        if (model.getName().length() == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "姓名不能为空");
        }

        if (model.getName().length() > 32) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "姓名不能超过32个字");
        }
        if (model.getIdentity() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款银行卡号不能为空");
        }
        model.setIdentity(model.getIdentity().trim());
        if (model.getIdentity().length() == 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款银行卡号不能为空");
        }

        if (model.getIdentity().length() > 32) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款银行卡号不能超过32个字");
        }
        if (!BankUtil.isValid(model.getIdentity())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "银行卡账号格式不正确，只支持13, 16, 17, 18, 19, 20位银行卡号");
        }
        if (Strings.isNullOrEmpty(model.getTransAmount())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款金额不能为空");
        }
        if (!MoneyCheckUtil.isValidMoney(model.getTransAmount(), 2)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款金额最多2位小数");
        }
        if (new BigDecimal(model.getTransAmount()).compareTo(new BigDecimal("0.1")) < 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款金额最必须大于等于0.1元");
        }
        if (new BigDecimal(model.getTransAmount()).compareTo(new BigDecimal("50000")) > 0) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "收款金额不能超过50000元");
        }

    }
}
